package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Log;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;

//提交登录的表单，验证登录信息与数据库是否相匹配
public class SignOnFormServlet extends HttpServlet {
    private static final String SIGN_ON = "/WEB-INF/jsp/catalog/Main.jsp";
    //需要更改，不同的错误显示不同的提示字符串
    private static final String ERROR = "/WEB-INF/jsp/account/signonForm.jsp";
    private Account account;
    private String username;
    private String password;
    private String validString;
    private String tip;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得用户输入的信息
        username = request.getParameter("username");
        password = request.getParameter("password");
        validString = request.getParameter("valistr");
        //获取数据库中的对应account，若无则为null

        AccountService accountService = new AccountService();
        account = accountService.getAccount(username, password);
        HttpSession session = request.getSession();

        System.out.println(username);
        System.out.println(password);

        //先判断账户是否存在
        if(account == null){
            //提示该用户不存在，请重新输入或注册新账号
            tip = "User not exist,Please Enter again or register a new account";
            session.setAttribute("tipInfo",tip);
            request.getRequestDispatcher(ERROR).forward(request, response);
            System.out.println("1");
        }
        //再判断验证码是否正确
        else{
            //若验证码为空，提示输入验证码
            if (validString.equals("")){
                tip = "Identifying code can't be null";
                session.setAttribute("tipInfo",tip);
                request.getRequestDispatcher(ERROR).forward(request, response);
                System.out.println("2");

            }else{
                //验证码验证
                String text = ValiImageServlet.text;
                //忽略大小写
                //正确
                if(text.compareToIgnoreCase(validString)==0){
                    session.setAttribute("account", account);
                    session.setAttribute("tipInfo",null);

                    //时间
                    SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String datetime = tempDate.format(new java.util.Date());
                    //信息
                    String info = "用户"+account.getUsername()+"登录";
                    Log log = new Log(account.getUsername(),datetime,info);
                    //添加log
                    new AccountService().addLog(log);

                    request.getRequestDispatcher(SIGN_ON).forward(request, response);
                    System.out.println("3");
                    System.out.println("should have"+account.getUsername()+"09"+account.getPassword());
                }
                //错误
                else{
                    //提示验证码输入错误
                    tip = " Identifying code is wrong";
                    session.setAttribute("tipInfo",tip);
                    request.getRequestDispatcher(ERROR).forward(request, response);
                }
            }
        }
    }
}
